package com.cqjtu.tto.controller;

import com.cqjtu.tto.dao.pojo.SysUser;
import com.cqjtu.tto.service.ScreenshotRecordService;
import com.cqjtu.tto.service.SysUserService;
import com.cqjtu.tto.service.impl.DeferredResultService;
import com.cqjtu.tto.vo.Results;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 监控控制类
 */
@RestController
@RequestMapping("monitor")
@Api(tags = {"监控接口"})
public class MonitorController {
    @Autowired
    private DeferredResultService deferredResultService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ScreenshotRecordService screenshotRecordService;

    // 默认客户端请求超时时间60s
    private final Long clientTimeOut = 60000l;
    // 默认前端请求超时时间5s
    private final Long webTimeOut = 5000l;


//    @ApiOperation(value="获取全部的监控记录", notes="获取全部的监控记录")
    @GetMapping("list")
    public Results listMonitorRecord(){
        return screenshotRecordService.listMonitorRecord();
    }


    @ApiOperation(value="获取全部员工的实时监控信息", notes="获取全部员工的实时监控信息")
    @GetMapping("listEmployeeMonitor")
    public Results listEmployeeMonitor(){
        return screenshotRecordService.listEmployeeMonitor();
    }
    @ApiOperation(value="根据员工id获取员工的历史监控记录", notes="根据员工id获取员工的历史监控记录")
    @GetMapping("getEmployeeByUserId")
    public Results  getEmployeeByUserId(@RequestParam("id")Long id){
        return screenshotRecordService.getEmployeeByUserId(id);
    }


    /**
     * 异步请求，客户端向服务器发起请求，等待服务器响应
     * @return
     */
//    @ApiOperation(value="客户端发出异步请求", notes="客户端发出请求，等待前端响应")
    @GetMapping("get")
    public DeferredResult<Results> get(HttpServletRequest request){
        String remoteAddr = request.getRemoteAddr();
        System.out.println(remoteAddr);
        DeferredResult<Results> deferredResult = new DeferredResult<>(clientTimeOut);

        if (!deferredResultService.judgeTaskExist(remoteAddr)){
            deferredResultService.process(remoteAddr, deferredResult);
        }
        else{
            deferredResult.setResult(Results.fail(1012,"客户端不存在"));
        }
        return deferredResult;
    }

    /**
     * 前端发出请求获取某个员工的实时截屏 @RequestParam("ip") String requestIp
     * @return
     */
    @ApiOperation(value="前端发出请求获取某个员工的实时截屏", notes="前端发出请求获取某个员工的实时截屏,参数为id")
    @GetMapping("getMonitor")
    public DeferredResult<Results> settingResult(@RequestParam("id") Long userId){
        // 根据用户id获取用户ip
        SysUser sysUser = sysUserService.getById(userId);
        String ip = sysUser.getIp();
        System.out.println("ip:"+ip);
        DeferredResult<Results> deferredResult = new DeferredResult<>(webTimeOut);
        // 响应客户端的请求
        // 先判断客户端是否存在
        if (deferredResultService.judgeTaskExist(ip)){
            // 存在
            Results results = new Results(true, 1017,"获取截屏", null);
            deferredResultService.settingResult(ip, results);
            // 等待客户端响应截图
            try{

                deferredResultService.process(ip, deferredResult);
            } catch (Exception e){
                e.printStackTrace();
            }
        }
        else{
            deferredResult.setResult(Results.fail(1018,"客户端不存在"));
        }
        return deferredResult;
    }
    /**
     * 接收客户端发来的截图
     */
//    @ApiOperation(value="客户端发来截图", notes="客户端发来截图，保存到服务器并响应给前端")
    @PostMapping("getScreenShot")
    public Results getScreenShot(HttpServletRequest request, HttpServletResponse response){
        String base64 = request.getParameter("base64");
        String remoteAddr = request.getRemoteAddr();
        System.out.println(remoteAddr);
        if (base64 != null){
            // 将图片返回到前端
            deferredResultService.settingResult(remoteAddr, Results.success(base64));
            deferredResultService.saveImg(remoteAddr, base64);  // 保存图片到服务器并生成记录
        }
        else{
            deferredResultService.settingResult(remoteAddr, Results.fail(1018,"SELECT_ERROR"));
            return Results.fail(1018,"失败");
        }
        return Results.success("成功");
    }
}
